<?php
namespace App\Api\V1\Models;

use Illuminate\Database\Eloquent\Model;

class Ads extends Model
{

    protected $table = 'ads';
    protected $primaryKey = 'ads_id';
    public $timestamps = false;
    /*
     * 添加一个广告
     * */
    public function addAdsItem()
    {
        $data =[
            'ads_position_id'=>'1',
            'code'=>'index_1',
            'name'=>'textb',
            'url'=>'',
            'target'=>'_self',
            'content'=>'',
            'color'=>'#ffffff',
            'begin_time'=>'2018-05-01 00:00:00',
            'end_time'=>'2018-05-03 23:59:59',
            'sort'=>'50',
            'status'=>'1'
        ];
        $this->ads_position_id = $data['ads_position_id'];
        $this->code = $data['code'];
        $this->name = $data['name'];
        $this->url = $data['url'];
        $this->target = $data['target'];
        $this->content = $data['content'];
        $this->color = $data['color'];
        $this->begin_time = $data['begin_time'];
        $this->end_time = $data['end_time'];
        $this->sort = $data['sort'];
        $this->status = $data['status'];
        if ($this->save()){
            return $this->toArray();
        }
        return false;

    }
    /*
     * 编辑一个广告
     * */
    public function setAdsItem(){
        $data =[
            'ads_position_id'=>'1',
            'code'=>'index_1',
            'name'=>'textb',
            'url'=>'',
            'target'=>'_self',
            'content'=>'',
            'color'=>'#ffffff',
            'begin_time'=>'2018-05-01 00:00:00',
            'end_time'=>'2018-05-03 23:59:59',
            'sort'=>'50',
            'status'=>'1'
        ];
        $res = Ads::where('ads_id',1)->update($data);
        if ($res){
            return $this->toArray();
        }
        return false;
    }
    /*
     * 批量删除广告
     * */
    public function delAdsList(){
        $ads_id = [113,114,115];
//        for ($i = 0;$i<=count($id);$i++){
            $res = Ads::destroy($ads_id);
//        }
        if($res){
            return $this->toArray();
        }
        return false;
    }
    /*
     * 设置广告排序
     * */
    public function setAdsSort(){
        $sort = 2;
        $id = 2;
        $data = ['sort'=>$sort];
        $res = Ads::where('ads_id',$id)->update($data);
        if ($res){
            return $this->toArray();
        }
        return false;
    }
    public function getAdsList(){
        return $this->get()->toArray();
    }
    /*
     * 根据编号自动排序
     * */
    public function setAdsIndex(){
        return $this->orderBy('sort', 'asc')->get();
    }
    /*
     * 批量设置是否显示
     * */
    public function setAdsStatus(){

    }

}
